<template>
  <div><div class="abstatic-height"></div>
    <div class="app-container calendar-list-containerab">
      <div class="top-handle-tab">
      <el-row style="padding-top: 10px;">
          <el-col :span="8">
            <el-radio-group v-model="radioType" @change="switchTable" style="float:left;">
                <el-radio-button label="now">当前报警</el-radio-button>
                <el-radio-button label="Fire_alarm">报警记录</el-radio-button>
            </el-radio-group>
          </el-col>
          <el-col :span="16">
            <div class="search" style="text-align:right;">
                <span style="font-size: 14px;">报警类型</span>
                <el-select v-model="listQuery.alarmType" filterable placeholder="请选择" size="mini">
                  <el-option label="全部" value></el-option>
                  <el-option
                    v-for="(item,index) in alrmTypeList"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <span style="font-size: 14px;margin-left: 20px;">报警时间</span>
                <el-date-picker
                  v-model="listQuery.dateStrs"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="—"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width:240px;"
                ></el-date-picker>
                <el-button
                  class="filter-item"
                  type="primary"
                  :disabled="isadvance"
                  @click="checkList"
                  size="mini"
                >查询</el-button>
                <el-popover placement="bottom" width="400" v-model="isadvance" trigger="click">
                  <el-button class="filter-item" slot="reference" @click.stop="advCheck" size="mini" type="text">高级查询</el-button>
                  <div class="advancequery">
                    <el-form
                      :model="formLabelAlign"
                      ref="searchForm"
                      :rules="advancerules"
                      label-width="100px"
                      label-position="top"
                    >
                      <el-form-item label="设备名" prop="sensorName">
                        <el-input v-model.trim="formLabelAlign.deviceName"></el-input>
                      </el-form-item>
                      <el-form-item label="系列" prop="equipmentType">
                        <el-select
                          v-model.trim="formLabelAlign.deviceSerial"
                          filterable
                          placeholder="全部"
                          style="width: 100%;"
                        >
                           <el-option label="全部" value></el-option>
                          <el-option
                            v-for="(item,index) in series"
                            :key="index"
                            :label="item"
                            :value="item"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item label="设备编号" prop="sensorNo">
                        <el-input v-model.trim="formLabelAlign.sensorNo"></el-input>
                      </el-form-item>
                      <el-form-item>
                        <el-button type="primary" @click="handleAdvaceSearch('searchForm')" size="mini">查询</el-button>
                        <el-button @click="resetForm('searchForm')" size="mini" type="text">取消</el-button>
                      </el-form-item>
                    </el-form>
                  </div>
                </el-popover>
            </div>
          </el-col>
      </el-row>
        <div class="line-with-verticl"></div>
      </div>
      <el-row>
        <el-table
          :data="list"
          :key="tableKey"
          v-loading.body="listLoading"
          style="width: 100%;margin-top: 10px;"
        >
          <el-table-column label="图片" style="text-align:center;" width="300">
            <template slot-scope="scope">
               <img src="@/assets/device-images/default300.png" v-if="scope.row.pictures==''||scope.row.pictures==undefined" alt="" style="width:300px;height:150px;">
                <viewer style="height:150px;position:relative;" v-else>
                    <img v-for="(item,index) in scope.row.pictures" :src="item" :key="index" width="300" height="150px" >
                    <div class="picNum" style="position:absolute;bottom:2px;left:46%;">{{scope.row.pictures.length}}张</div>
                </viewer>
            </template>
          </el-table-column>
          <el-table-column label="设备名">
            <template slot-scope="scope">
              <span>{{scope.row.deviceName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="设备编号">
            <template slot-scope="scope">
              <span>{{scope.row.sensorNo}}</span>
            </template>
          </el-table-column>
           <el-table-column label="通道号">
            <template slot-scope="scope">
              <span>{{scope.row.channelNo}}</span>
            </template>
          </el-table-column>
          <el-table-column label="系列">
            <template slot-scope="scope">
              <span>{{scope.row.deviceSerial}}</span>
            </template>
          </el-table-column>
          <el-table-column label="报警类型">
            <template slot-scope="scope">
              <span style="color:#FF3B30;">{{scope.row.alarmType}}</span>
            </template>
          </el-table-column>
          <el-table-column label="报警时间">
            <template slot-scope="scope">
              <span>{{scope.row.alarmTime}}</span>
            </template>
          </el-table-column>
          <el-table-column label="恢复时间" v-if="resShow">
            <template slot-scope="scope">
              <span>{{scope.row.restoreTime}}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <div class="pagination-container" v-show="!listLoading" >
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="listQuery.page"
              :page-sizes="[15,20,30, 50]"
              :page-size="listQuery.limit"
              layout="prev, pager, next, jumper ,total"
              :total="total">
          </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import {
  page,
  selectAlrmType,
  deviceSerials
} from "@/api/device/deviceAbnormal/index";
export default {
  data() {
    return {
      resShow:false,
      radioType:'now',
      total:0,
      listQuery: {
        page: 1,
        limit: 15,
        alarmType:"",
        dateStrs:"",
        hasRestore:false,
      },
      curChannelId:"",
      tableKey: 0,
      advancerules: {},
      listLoading: true,
      list: [],
      //  类型
      alrmTypeList: [],
      isadvance: false,
      series: [],
      formLabelAlign: {
        deviceName: "",
        deviceSerial: "",
        sensorNo: ""
      }
    };
  },
  mounted() {
    this.fetchData();
  },
  watch: {
    $route: "fetchData"
  },
  methods: {
    //清空高级查询内容
    resetPro(){
      this.formLabelAlign.deviceName='';
      this.formLabelAlign.deviceSerial='';
      this.formLabelAlign.sensorNo='';
    },
    checkList(){
      this.resetPro();
      this.getList();
    },
    advCheck(){
      this.isadvance = true;
      this.resetPro();
      deviceSerials().then(res => {
        this.series = res.data;
      })
    },
    handleSizeChange(val){
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.page = val;
      this.getList();
    },
    fetchData(){
        this.curChannelId = this.$route.params.id;
        this.initLoad();
    },
    initLoad(){
        this.getList();
        selectAlrmType().then(res =>{
            this.alrmTypeList = res.data;
        })
    },
    getList(){

        this.listLoading = true;

        this.list=[];
        let dateStrs;
        if(this.listQuery.dateStrs){
          dateStrs = (this.listQuery.dateStrs).toString();
        }
        let canshu = Object.assign({},this.listQuery,this.formLabelAlign,{dateStrs:dateStrs})
        page(canshu).then(res => {
            this.list = res.data.rows;
            this.total = res.data.total;
            this.listLoading = false;
        }).catch(() => {
            this.listLoading = false;
        });
    },
    switchTable() {
      if(this.radioType == 'now'){

        this.listQuery.hasRestore = false;

        this.getList();
        this.resShow = false;
      }else{
         this.listQuery.hasRestore = true;

          this.getList();
          this.resShow = true;
      }
    },
    handleFilter() {
      this.getList();
    },
    handleAdvaceSearch(formName) {
        this.listQuery.page = 1;
        const set = this.$refs;

        let dateStrs;
        this.listLoading = true;
        if(this.listQuery.dateStrs){
          dateStrs = (this.listQuery.dateStrs).toString();
        }
        let canshu = Object.assign({},this.listQuery,this.formLabelAlign,{dateStrs:dateStrs})
        page(canshu).then(res => {
          this.list = res.data.rows;
          this.total = res.data.total;
          this.listLoading = false;
        }).catch(() =>{
          this.listLoading = false;
        })
        this.isadvance=false;

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
       this.resetPro();
      this.isadvance = false;
    }
  }
};
</script>
<style lang="scss" rel="stylesheet/scss">
// .search {
//   float: right;
// }
// .inside{
//     display: none;
// }
.calendar-list-containerab{
  padding: 20px;
}
.picNum{
  width: 42px;
  height: 16px;
  line-height: 16px;
  opacity: 0.54;
  border-radius: 8px;
  background-color: rgba(0, 0, 0, 1);
  text-align: center;
  color: #fff;
  font-size: 12px;
}
</style>

